package top.codedance.iotp.tools.serverTool.subframe;

import top.codedance.iotp.iot.sdk.entity.UserEntity;
import top.codedance.iotp.tools.owner.ToolCallOwner;
import top.codedance.iotp.tools.serverTool.ui.AddUserFrame;
import top.codedance.iotp.tools.serverTool.ui.GenDeviceFrame;
import top.codedance.iotp.tools.terminal.ui.UserVerificationCertificateManager;

import javax.swing.*;
import java.net.URL;
import java.util.UUID;

/**
 *
 * @author Lenovo
 */
public class AddUserSubFrame extends javax.swing.JDialog implements ToolCallOwner {
    private AddUserFrame addUserFrame;

    /**
     * Creates new form AddUserSubFrame
     */
    public AddUserSubFrame(AddUserFrame addUserFrame) {
        super(addUserFrame, true);
        initComponents();
        this.addUserFrame = addUserFrame;
        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setLocationRelativeTo(this.addUserFrame.getMainFrame().getShellFrame());
        setTitle("代码飞舞采控系统-添加用户");
    }

    @Override
    public void setValue(String value, int type) {
        switch (type) {
            case 0:
                deviceIdTf.setText(value);
                break;
            case 1:
                passwordTf.setText(value);
                break;
        }
    }

    @Override
    public String getValue(int type) {
        String result = "";
        switch (type) {
            case 0:
                result = deviceIdTf.getText();
                break;
            case 1:
                result = passwordTf.getText();
                break;
        }
        return result;
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bg1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        deviceIdTf = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        usernameTf = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        passwordTf = new javax.swing.JTextField();
        addBtn = new javax.swing.JButton();
        jb1 = new javax.swing.JRadioButton();
        jb2 = new javax.swing.JRadioButton();
        jb_IdEnOrDeCode = new javax.swing.JButton();
        jb_genId = new javax.swing.JButton();
        jb_passwordEnOrDeCode = new javax.swing.JButton();
        jb_genPassword = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("设备Id: ");

        jLabel2.setText("是否是管理员: ");

        jLabel3.setText("用户名: ");

        jLabel4.setText("密码: ");

        addBtn.setLabel("添加");
        addBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBtnActionPerformed(evt);
            }
        });

        bg1.add(jb1);
        jb1.setText("是");
        jb1.setActionCommand("1");

        bg1.add(jb2);
        jb2.setSelected(true);
        jb2.setText("否");
        jb2.setActionCommand("0");

        jb_IdEnOrDeCode.setText("加解密...");
        jb_IdEnOrDeCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_IdEnOrDeCodeActionPerformed(evt);
            }
        });

        jb_genId.setText("生成ID...");
        jb_genId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_genIdActionPerformed(evt);
            }
        });

        jb_passwordEnOrDeCode.setText("加解密...");
        jb_passwordEnOrDeCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_passwordEnOrDeCodeActionPerformed(evt);
            }
        });

        jb_genPassword.setText("生成密码");
        jb_genPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_genPasswordActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(usernameTf)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jb1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jb2)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(deviceIdTf)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_genId)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_IdEnOrDeCode))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(passwordTf)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_genPassword)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_passwordEnOrDeCode)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(306, 306, 306)
                .addComponent(addBtn)
                .addContainerGap(322, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(deviceIdTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jb_IdEnOrDeCode)
                    .addComponent(jb_genId))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jb1)
                    .addComponent(jb2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(usernameTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(passwordTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jb_passwordEnOrDeCode)
                    .addComponent(jb_genPassword))
                .addGap(18, 18, 18)
                .addComponent(addBtn)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBtnActionPerformed
        if(!deviceIdTf.getText().equals("") && !passwordTf.getText().equals("") && !usernameTf.getText().equals("")) {
            UserEntity userEntity = new UserEntity();
            userEntity.setDeviceId(deviceIdTf.getText());
            userEntity.setIsAdmin(Integer.parseInt(bg1.getSelection().getActionCommand()));
            userEntity.setPassword(passwordTf.getText());
            userEntity.setUsername(usernameTf.getText());
            addUserFrame.addAction(userEntity);
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(rootPane, "设备号、用户名和密码都不能为空。");
        }
    }//GEN-LAST:event_addBtnActionPerformed

    private void jb_IdEnOrDeCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_IdEnOrDeCodeActionPerformed
        UserVerificationCertificateManager uvcm = new UserVerificationCertificateManager(addUserFrame.getMainFrame().getShellFrame(), this, 0);
        uvcm.setLocationRelativeTo(addUserFrame.getMainFrame().getShellFrame());
        uvcm.setVisible(true);
    }//GEN-LAST:event_jb_IdEnOrDeCodeActionPerformed

    private void jb_genIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_genIdActionPerformed
        GenDeviceFrame gdf = new GenDeviceFrame(addUserFrame.getMainFrame(), this, 0);
        gdf.setVisible(true);
    }//GEN-LAST:event_jb_genIdActionPerformed

    private void jb_passwordEnOrDeCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_passwordEnOrDeCodeActionPerformed
        UserVerificationCertificateManager uvcm = new UserVerificationCertificateManager(addUserFrame.getMainFrame().getShellFrame(), this, 1);
        uvcm.setLocationRelativeTo(addUserFrame.getMainFrame().getShellFrame());
        uvcm.setVisible(true);
    }//GEN-LAST:event_jb_passwordEnOrDeCodeActionPerformed

    private void jb_genPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_genPasswordActionPerformed
        passwordTf.setText(UUID.randomUUID().toString());
    }//GEN-LAST:event_jb_genPasswordActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addBtn;
    private javax.swing.ButtonGroup bg1;
    private javax.swing.JTextField deviceIdTf;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JRadioButton jb1;
    private javax.swing.JRadioButton jb2;
    private javax.swing.JButton jb_IdEnOrDeCode;
    private javax.swing.JButton jb_genId;
    private javax.swing.JButton jb_genPassword;
    private javax.swing.JButton jb_passwordEnOrDeCode;
    private javax.swing.JTextField passwordTf;
    private javax.swing.JTextField usernameTf;
    // End of variables declaration//GEN-END:variables
}
